import wave

import pyaudio
from scipy.io import wavfile

from melo.api import TTS

tts_config_path = r"C:\tools\projects\python\role-bot\config\voice\ZHconfig.json"
tts_device = "cuda:0"
tts_speed = 1
speak_name = r"C:\tools\projects\python\role-bot\config\voice\W-X1.pth"
tts_model = TTS(language='ZH', config_path=tts_config_path,
                ckpt_path=speak_name, device=tts_device)
speaker_ids = tts_model.hps.data.spk2id
voice = tts_model.tts_to_file("你好，我是台湾妹子，很高兴认识你。", speaker_ids['ZH'], speed=tts_speed)
print(tts_model.hps.data.sampling_rate)
print(type(tts_model.hps.data.sampling_rate))
wavfile.write('湾妹音频.wav', 44100, voice)
p = pyaudio.PyAudio()
stream = p.open(
    format=pyaudio.paFloat32,
    channels=1,
    rate=tts_model.hps.data.sampling_rate,
    output=True
)

# 将numpy数组转为字节数据并播放
stream.write(voice.tobytes())

# 清理资源
stream.stop_stream()
stream.close()
p.terminate()
